package com.example.wangandroid.adapter

import android.view.LayoutInflater
import android.view.TextureView
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.utils.widget.ImageFilterView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide

import com.example.datas.ProjectList
import com.example.wangandroid.R
import com.example.wangandroid.datas.ProjectData

class ProjectAdapter(var fragment:Fragment,var data:ProjectList): RecyclerView.Adapter<ProjectAdapter.ViewHoder>() {

    class  ViewHoder(itemView: View) :RecyclerView.ViewHolder(itemView){
        var title:TextView = itemView.findViewById(R.id.tv_title)
        var dec :TextView = itemView.findViewById(R.id.tv_dec)
        var iv:ImageFilterView = itemView.findViewById(R.id.iv_image)
        var ll:LinearLayout = itemView.findViewById(R.id.ll)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHoder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.project_data,parent,false)
        return ProjectAdapter.ViewHoder(view)
    }

    override fun getItemCount(): Int {
       return data.data.datas.size
    }

    override fun onBindViewHolder(holder: ViewHoder, position: Int) {
        if(position>=data.data.datas.size){
            return
        }
        holder.dec.text = data.data.datas[position].desc
        holder.title.text = data.data.datas[position].title
        Glide.with(fragment).load(data.data.datas[position].envelopePic).into(holder.iv)
        holder.ll.setOnClickListener {
            ARouter.getInstance().build("/app/article").withString("url",data.data.datas[position].link).navigation()
        }
    }
}